import java.util.*;

public class VehicleQueue implements Observer{

	private double vehiclesPerSecondEnter;
	private int trafficLightRate;
	private double vehiclesPerSecondLeave;
	private boolean greenLight=false;
	private VehicleGenerator generator;
	
	private List<Vehicle> queue;
	
	public VehicleQueue(double vehiclesPerSecondEnter, int trafficLightRate,
			double vehiclesPerSecondLeave, VehicleGenerator vg){
		this.vehiclesPerSecondEnter=vehiclesPerSecondEnter;
		this.trafficLightRate=trafficLightRate;
		this.vehiclesPerSecondLeave=vehiclesPerSecondLeave;
		generator=vg;
	
		queue = new ArrayList<Vehicle>();
	}
	
	public void enter(){
		int temp = (int)(trafficLightRate/vehiclesPerSecondEnter);
		for(int i=0;i<temp;i++){
			queue.add(generator.createVehicle());
		}
		
	}
	
	public void leave(){
		int temp = (int)(trafficLightRate/vehiclesPerSecondLeave);
		for(int i=0;i<temp;i++){
			queue.remove(queue.getClass());
		}
	}
	
	public double getLength(){
		double length = 0;
		for(Vehicle it : queue){
			length=length+it.getLength();
		}
		return length;
	}
	
	public int getSize(){
		return queue.size();
	}
	
	public void update(Observable o, Object time){
		
		int t = (Integer)time;
		
		if(greenLight==false)
				enter();
		if(greenLight==true){
			enter();	
			leave();
		}
		
		if((t%trafficLightRate)==0){
			greenLight=!greenLight;
		}
	}

	
}
